// pages/my_address/my_address.js
import config from "../../utils/config";
import {request} from "../../utils/request";
const app = getApp()
Page({
    data: {
        custom:wx.getMenuButtonBoundingClientRect(),
        list:[]
    },
    onLoad(options) {

    },
    onShow(){
        console.log(1)
        this.getList()
    },
    getList(){
        var user = wx.getStorageSync('userInfo')
        request({url:'/miniprogram/user/getAddressList',data:{user_id:user.id}}).then(res=>{
            this.setData({list:res.data})
        })
    },
    setmain(e){
        var {id, main} = e.currentTarget.dataset
        var user = wx.getStorageSync('userInfo')
        request({url:'/miniprogram/user/setAddressMain',data:{id,user_id:user.id,main:main == 1 ? 0 : 1}}).then(res=>{
            this.getList()
        })
    },
    handle(e){
        var id = e.currentTarget.dataset.id
        wx.showActionSheet({
            itemList: ['编辑', '删除'],
            success:res=>{
                if(res.tapIndex == 0){
                    wx.navigateTo({url:"/pages/edit_address/edit_address?id="+id})
                }else{
                    request({url:"/miniprogram/user/delAddress",data:{id}}).then(res=>{
                        this.getList()
                    })
                }
            }
        })
    },
    choose(e){
        var index = e.currentTarget.dataset.index
        var pages = getCurrentPages()
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
            address: this.data.list[index]
        })
        wx.navigateBack({delta:1})
    }
})